<?php

namespace plugins\module_config\controller;

use cmf\controller\PluginAdminBaseController; //引入此类

use think\Cache;
use tree\Tree;

/**

 * 文档控制类

 */

class HelpFileController extends PluginAdminBaseController
{
    //请求数据接口
    protected $getList = 'https://gl.300c.cn/plugin/word/api_index/getList';
    //详情
    protected $getContent = 'https://gl.300c.cn/plugin/word/api_index/wordcontent';

    //根据 id取出当前和下级文档
    public function index()
    {
        $data = $this->request->param();

        if (empty($data['id'])) {

            return api_json_echo(false, 'id不能为空', null, -1);

        }

        $id = $data['id'];

        $this->assign('id', $id);

        return $this->fetch();

        $tree = new Tree();

        $tree->icon = ['&nbsp;&nbsp;&nbsp;&nbsp│', '&nbsp;&nbsp;&nbsp;&nbsp;├', '&nbsp;&nbsp;&nbsp;&nbsp;└'];

        if (Cache::get('name') == false) {

            $url = $this->getList;

            $ret = file_get_contents($url);

            $retarr = json_decode($ret, true);

            $list = $retarr['data'];

            Cache::set('name', $list, 3600);

        } else {

            $list = Cache::get('name');

        }

        foreach ($list as $key => $value) {

            $value['selected'] = '';

            if ($value['id'] == $id) {

                $value['selected'] = 'selected';

            }

            if ($value['type'] == '1') {

                $value['disabled'] = "disabled='disabled'";

                $value['icon'] = "📁";

            } else {

                $value['disabled'] = "";

                $value['icon'] = "📄";

            }

            $list[$key] = $value;

        }

        $str = "<option value=\$id \$selected \$disabled>\$spacer \$icon \$name</option>";

        //内容放入

        $tree->init($list);

        $select = $tree->getTree(0, $str); //

        $this->assign('select', $select);

        return $this->fetch();

    }

    /**

     * post请求

     */

    public function curl_file_post_contents($durl, $post_data)
    {

        // header传送格式

        //初始化

        $curl = curl_init();

        //设置抓取的url

        curl_setopt($curl, CURLOPT_URL, $durl);

        //设置头文件的信息作为数据流输出

        curl_setopt($curl, CURLOPT_HEADER, false);

        //设置获取的信息以文件流的形式返回，而不是直接输出。

        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

        //设置post方式提交

        curl_setopt($curl, CURLOPT_POST, true);

        // 设置post请求参数

        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);

        // 添加头信息

        // CURLINFO_HEADER_OUT选项可以拿到请求头信息

        curl_setopt($curl, CURLINFO_HEADER_OUT, true);

        // 不验证SSL

        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);

        //执行命令

        $data = curl_exec($curl);

        // 打印请求头信息

        // echo curl_getinfo($curl, CURLINFO_HEADER_OUT);

        //关闭URL请求

        curl_close($curl);

        //显示获得的数据

        return $data;

    }

    //得到对应的文章内容

    public function wordContent()
    {

        $param = $this->request->get();

        $url = $this->getContent;

        $post_data = array('id' => $param['id']);

        $ret = $this->curl_file_post_contents($url, $post_data);

        $a = json_decode($ret, true);

        if ($a['status'] == 'error') {

            return json(['type' => 'error', 'msg' => '查询出错']);

        } else {

            $info = json_decode($ret, true);

            //文本化

            $info['data']['content'] = html_entity_decode($info['data']['content']);

            if ($info) {

                return json(['type' => 'success', 'msg' => '查询成功', 'info' => $info['data']]);

            } else {

                return json(['type' => 'error', 'msg' => '查询出错']);

            }

        }

        $info = json_decode($ret, true);

        //文本化

        $info['data']['content'] = html_entity_decode($info['data']['content']);

        if ($info) {

            return json(['type' => 'success', 'msg' => '查询成功', 'info' => $info['data']]);

        } else {

            return json(['type' => 'error', 'msg' => '查询出错']);

        }

    }

}
